using System;
using System.Collections;
using System.Collections.Generic;
using HyxFrame.Core;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.SceneManagement;
using UnityEngine.UI;



/*
*Creater:Hyx
*Introduce:介绍
*Version:0.0.1
*ChangeLog:创建--2024.01.01
*/

namespace Hyx.UI
{

    public class LoadingPanel : UIBase
    {



        public Image bg_Image;
        public Slider m_Slider;

        public Text m_Text;

        private int startShowProgess = 5;

        private Dictionary<string, string> keyLoadingBG = new Dictionary<string, string>();
        private AsyncOperationHandle<Sprite> curLoadBGHandle;


        private Action action;
        private string nextScene;
        private string sceneBg;

        private AsyncOperationHandle<SceneInstance> curSceneHandle;

        private void Awake()
        {
            if (m_Slider == null)
            {
                m_Slider = transform.Find("Component/Slider").GetComponent<Slider>();
            }
            if (m_Text == null)
            {
                m_Text = transform.Find("Component/Text").GetComponent<Text>();
            }
            m_Slider.value = 0;
            m_Text.text = "";
        }


        protected override void OnStart()
        {
            nextScene = Data.GetString(0);
            sceneBg = Data.GetString(1);
            action = Data.GetAction(2);

            StartCoroutine(LoadSceneAsync());
        }


        IEnumerator LoadSceneAsync()
        {
            LoadBGAsset(sceneBg);         // 加载资源图

            yield return new WaitForEndOfFrame();               //加上这么一句就可以先显示加载画面然后再进行加载
            int displayProgress = startShowProgess;

            int toProgress = (int)(UnityEngine.Random.value * 88);            //假随机卡在某个数
                                                                              //假进度前88%
            while (displayProgress < toProgress)
            {
                SetLoadingPercentage(++displayProgress);
                yield return new WaitForEndOfFrame();
            }
            yield return new WaitForSeconds(0.2f);                 //把卡顿推后,假装是进度中的,防止一开始玩家就跑了<过半理论>

            if (nextScene != SceneManager.GetActiveScene().name)        // 不同场景的切换
            {
                yield return new WaitForSeconds(0.2f);                 //把卡顿推后,假装是进度中的,防止一开始玩家就跑了<过半理论>

                curSceneHandle = Addressables.LoadSceneAsync(nextScene);
                while (!curSceneHandle.IsDone)
                {
                    toProgress = (int)(curSceneHandle.PercentComplete * 100);
                    while (displayProgress < toProgress)
                    {
                        SetLoadingPercentage(++displayProgress);
                        yield return new WaitForEndOfFrame();
                    }
                    yield return new WaitForEndOfFrame();
                }

                yield return null;
                yield return null;

                EventDispatcher.TriggerEvent((int)EventTypeListBase.SceneChangeFinish, nextScene);
            }

            action?.Invoke();

            toProgress = 100;
            while (displayProgress < toProgress)
            {
                SetLoadingPercentage(++displayProgress);
                yield return new WaitForEndOfFrame();
            }
            yield return new WaitForEndOfFrame();               //加上这么一句就可以先显示加载画面然后再进行加载

            if (curLoadBGHandle.IsValid())
            {
                Debug.Log("curLoadBGHandle");
                Addressables.Release(curLoadBGHandle);
            }

            UIManager.Instance.ClosePanel("LoadingPanel");
        }

        void SetLoadingPercentage(int sliderProgress)
        {
            m_Slider.value = sliderProgress * 0.01f;
            m_Text.text = "加载资源 " + sliderProgress.ToString() + "%";
        }


        /// <summary>
        /// 加载对应场景名字的背景图片
        /// </summary>
        /// <param name="name"></param>
        public void LoadBGAsset(string name)
        {
            ResourceManager.Instance.LoadSprite($"LoadingBG/{name}.png", (s) =>
            {
                if (s != null)
                {
                    bg_Image.sprite = s;
                }
                else
                {
                    bg_Image.sprite = null;
                    Debug.LogError($"LoadBGAsset failed! {name}");
                }
            });

        }




    }

}
